//
//  GuideVC.swift
//  引导页
//
//  Created by 千策万划-mini on 2025/4/28.
//

import Foundation

class GuideVC: BaseVC, UIScrollViewDelegate {
    
    // MARK: - lazy
    lazy var scrollView: UIScrollView = {
        let result = UIScrollView()
        result.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT)
        result.isPagingEnabled = true;
        result.backgroundColor = COLOR_PRIMARY()
        result.isUserInteractionEnabled = true
        result.showsVerticalScrollIndicator = false
        result.showsHorizontalScrollIndicator = false
        result.bounces = false
        result.delegate = self
        return result
    }()
    lazy var imageLeft: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT)
        result.image = UIImage(named: "icon_guide_1")
        result.contentMode = .scaleAspectFill;
        result.clipsToBounds = true;
        return result
    }()
    lazy var imageRight: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT)
        result.image = UIImage(named: "icon_guide_2")
        result.contentMode = .scaleAspectFill;
        result.clipsToBounds = true;
        return result
    }()
    lazy var btnNext: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(163, 58)
        result.setImage(UIImage(named: "icon_guide_next"), for: .normal)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var btnEnter: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(163, 58)
        result.isHidden = true
        result.setImage(UIImage(named: "icon_guide_enter"), for: .normal)
        result.tag = 2
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    
    // MARK: - viewDidLoad
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        GlobalMethod.getIDFA()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
    }
    
    func configUI() {
        viewBg.backgroundColor = COLOR_PRIMARY()
        scrollView.leftTop = XY(0, 0)
        imageLeft.leftTop = XY(0, 0)
        imageRight.leftTop = XY(SCREEN_WIDTH, 0)
        scrollView.addSubview(imageLeft)
        scrollView.addSubview(imageRight)
        scrollView.contentSize = CGSize(width: SCREEN_WIDTH*2, height: 0)
        
        view.addSubview(scrollView)
        btnNext.centerXBottom = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT-getBottomSafeAreaHeight()-50)
        view.addSubview(btnNext)
        btnEnter.centerXCenterY = btnNext.centerXCenterY
        view.addSubview(btnEnter)
    }
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1:
            let offset = CGPoint(x: SCREEN_WIDTH, y: 0)
            scrollView.setContentOffset(offset, animated: true)
            btnEnter.isHidden = false
            btnNext.isHidden = true
            break
        case 2:
            GlobalMethod.writeBool(true, forKey: LOCAL_FIRST_INSTALL, isPrefixed: false)
            GlobalMethod.createRootNav()
            break
        default:
            break
        }
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / SCREEN_WIDTH)
        btnNext.isHidden = page == 1
        btnEnter.isHidden = !btnNext.isHidden
    }
}
